package
{
    import com.questetra.bpms.air.model.FormData;
    import com.questetra.bpms.air.model.ProcessDataDefinition;
    import com.questetra.bpms.air.model.SelectItem;
    
    import flash.display.DisplayObject;
    import flash.utils.Dictionary;
    
    import mx.containers.HBox;
    import mx.controls.Label;
    import mx.controls.RadioButton;
    import mx.controls.TextArea;
    import mx.controls.TextInput;
    
    public class FormDataManager
    {
        /** singleton */
        private static var instance:FormDataManager;
        /** Singletonを保障するための文字列定数 */
        private static const SINGLETON_KEY:String = "FormDataManagerSingletonKey";
        
        /** FormDataとinputObjectを対応づけた集合 */
        private static var _inputObjectDictionary:Dictionary = new Dictionary(true);

        public function FormDataManager(singletonKey:String)
        {
            if (singletonKey != FormDataManager.SINGLETON_KEY)
            {
                throw new ArgumentError("getInstance()でインスタンスを取得してください。");
            }
        }

        public static function getInstance():FormDataManager
        {
            if (!instance)
            {
                instance = new FormDataManager(FormDataManager.SINGLETON_KEY);
            }
            return instance;
        }

        public function createInputObject(formData:FormData):DisplayObject
        {
            var hBox:HBox = new HBox();
//            hBox.setStyle("borderStyle", "solid");
            var label:Label = new Label();
            label.text = formData.name;
            hBox.addChild(label);

            //MEMO:各データ型ごとにモデルを準備したりまではしない
//            if ((formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING) && (formData.formType == ProcessDataDefinition.DATA_FORM_TYPE_TEXTFIELD))
            if (((formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING) && (formData.formType == ProcessDataDefinition.DATA_FORM_TYPE_TEXTFIELD)) ||
//                (formData.dataType == ProcessDataDefinition.DATA_TYPE_DATE) ||
//                (formData.dataType == ProcessDataDefinition.DATA_TYPE_DATETIME) ||
                (formData.dataType == ProcessDataDefinition.DATA_TYPE_DECIMAL))
            {
                var textInput:TextInput = new TextInput();
                textInput.text = formData.value;
                hBox.addChild(textInput);
                _inputObjectDictionary[formData] = textInput;
            }
            else if ((formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING) && (formData.formType == ProcessDataDefinition.DATA_FORM_TYPE_TEXTAREA))
            {
                var textArea:TextArea = new TextArea();
                textArea.text = formData.value;
                hBox.addChild(textArea);
                _inputObjectDictionary[formData] = textArea;
            }
//            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_DISCUSSION)
//            {
//                var outputText:Text = new Text();
//                outputText.text = formData.value;
//                outputText.width = 200;     //仮値
//                hBox.addChild(outputText);
//                var textArea2:TextArea = new TextArea();
//                hBox.addChild(textArea2);
//                _inputObjectDictionary[formData] = textArea2;
//            }
            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_SELECT)
            {
//TODO選択UIの種類による切り替え
                var array:Array /* of RadioButton */ = new Array();
                for each(var item:SelectItem in formData.items)
                {
                    var radioButton:RadioButton = new RadioButton();
                    radioButton.id = item.id;
                    radioButton.label = item.value;
                    hBox.addChild(radioButton);
                    array.push(radioButton);
                }
                _inputObjectDictionary[formData] = array;
            }
//            else if ((formData.dataType == ProcessDataDefinition.DATA_TYPE_QUSER) ||
//                        (formData.dataType == ProcessDataDefinition.DATA_TYPE_SELECT))
//            {
//            }
//            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_LIST)
//            {
//            }
//            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_NEW_FILE)
//            {
//            }
//            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_HTML_PANEL)
//            {
//            }
//            else
//            {
//            }

            return hBox;
        }

        public function getValue(formData:FormData):String
        {
//            if ((formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING) && (formData.formType == ProcessDataDefinition.DATA_FORM_TYPE_TEXTFIELD))
            if (((formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING) && (formData.formType == ProcessDataDefinition.DATA_FORM_TYPE_TEXTFIELD)) ||
                (formData.dataType == ProcessDataDefinition.DATA_TYPE_DECIMAL))
            {
                return TextInput(_inputObjectDictionary[formData]).text;
            }
            else if ((formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING) && (formData.formType == ProcessDataDefinition.DATA_FORM_TYPE_TEXTAREA))
            {
                return TextArea(_inputObjectDictionary[formData]).text;
            }
//            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_DISCUSSION)
//            {
//                return TextArea(_inputObjectDictionary[formData]).text;
//            }
            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_SELECT)
            {
//TODO複数の場合まだ
                var id:String = "";
//                var array:Array = Array(_inputObjectDictionary[formData]);
                var array:Array = _inputObjectDictionary[formData];
                for each(var radioButton:RadioButton in array)
                {
                    if (radioButton.selected == true)
                    {
                        id = radioButton.id;
                    }
                }
                return id;
            }
//            else if (formData.dataType == ProcessDataDefinition.)
//            {
//            }

            return "";
        }

        public static function getParameterName(formData:FormData):String
        {
//            if (formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING)
            if ((formData.dataType == ProcessDataDefinition.DATA_TYPE_STRING) ||
                (formData.dataType == ProcessDataDefinition.DATA_TYPE_DECIMAL))
            {
                return "data%5B" + formData.number + "%5D.input";
            }
//            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_DISCUSSION)
//            {
//                return "data%5B" + formData.number + "%5D.console";
//            }
            else if (formData.dataType == ProcessDataDefinition.DATA_TYPE_SELECT)
            {
//                return "data%5B" + formData.number + "%5D.selects";
                return "data%5B" + formData.number + "%5D.input";
            }
//            else if (formData.dataType == ProcessDataDefinition.)
//            {
//                return "data%5B" + formData.number + "%5D.";
//            }
            return "";
        }
    }
}